package com.gitee.cashzhang27.test.cloud.seata.user.service;

import com.gitee.cashzhang27.test.cloud.seata.user.entity.AccountTbl;
import com.gitee.cashzhang27.test.cloud.seata.user.mapper.AccountMapper;
import java.math.BigDecimal;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description：
 *
 * @author fangliangsheng
 * @date 2019-04-05
 */
@Service
@AllArgsConstructor
public class AccountService {

  private static final String ERROR_USER_ID = "1002";

  private AccountMapper accountDAO;

  @Transactional(rollbackFor = Exception.class)
  public void debit(String userId, BigDecimal num) {
    AccountTbl account = accountDAO.selectById(userId);
    account.setMoney(account.getMoney().subtract(num));
    accountDAO.updateById(account);

    if (ERROR_USER_ID.equals(userId)) {
      throw new RuntimeException("account branch exception");
    }
  }
}
